﻿package com.lz.football.module.framework
{
	/**
	 * Async Command (异步)一个个按顺序执行
	 * @author lake
	 * var asyncCmd :AsyncCommand = new AsyncCommand(2,new TCmd1(1),new TCmd2(1),new TCmd3(1));
			asyncCmd.start();
		TCmd1 TCmd2 TCmd3 继承 RBCommand
	 */	
    public class AsyncCommand extends RBCommand 
	{
        private var _commands:Vector.<RBCommand>;
        private var _numCommandCompleted:int;
		
        public function AsyncCommand(t:Number, ... _args)
		{
            super(t);
            _numCommandCompleted = 0;
			var command:*;
            _commands = new Vector.<RBCommand>();
            for each (command in _args)
			{
                _commands.push(RBCommand(command));
            }
        }
        final override public function execute():void
		{
            launchNextCommand();
        }
        private function oneCommandComplete():void
		{
            _numCommandCompleted++;
            if (_numCommandCompleted == _commands.length)
			{
                complete();
            } else 
			{
                launchNextCommand();
            }
        }
        private function launchNextCommand():void
		{
            _commands[_numCommandCompleted].callBack = oneCommandComplete;
            _commands[_numCommandCompleted].start();
        }
    }
}
